<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * 基础设置
 * Class Base
 * @author heyu
 */
class Config extends MY_Controller {
    /**
     * @var AdmSettingModel
     */
    public $mSetting = null;

	public function __construct(){
		parent::__construct();
        $this->loadModel('AdmSettingModel', 'mSetting');
	}
	
	/**
	 * 首页
	 */
	public function index(){

		if($_POST){
			$this->_save();
        }
        //修改前
       /* $info = $this->mSetting->get('customer_tel,min_withdraw_cash_amount,deliveryman_range,
        		map_key,base_distribution_fee,base_distribution_distance,exceed_base_distance_number,
        		exceed_base_weight_number,postion_time,deliveryman_comment_tags,
        		base_distribution_weight,exceed_base_distance,exceed_base_weight,rob_expect_time,
                sharp_title,sharp_url,sharp_icon,sharp_desc,app_android_update,app_ios_update', 'deliveryman');*/
        //修改后
        $info = $this->mSetting->get("","deliveryman");
        $info['deliveryman_comment_tags'] = empty($info['deliveryman_comment_tags']) ?  array() : json_decode($info['deliveryman_comment_tags'], true);
        //ios更新信息
      /*  $app_ios_update = $info['app_ios_update']?json_decode($info['app_ios_update'], true):array();
        $info['ios_version'] = isset($app_ios_update['version'])?$app_ios_update['version']:'';
        $info['ios_update_description'] = isset($app_ios_update['description'])?$app_ios_update['description']:'';
        $info['ios_is_forced'] = isset($app_ios_update['is_forced'])?$app_ios_update['is_forced']:0;
        $info['ios_update_url'] = isset($app_ios_update['download_url'])?$app_ios_update['download_url']:'';*/

        //安卓更新信息
      /*  $app_android_update = $info['app_android_update']?json_decode($info['app_android_update'], true):array();
        $info['android_is_forced'] = isset($app_android_update['is_forced'])?$app_android_update['is_forced']:0;
        $info['android_version'] = isset($app_android_update['version'])?$app_android_update['version']:'';
        $info['android_version_code'] = isset($app_android_update['code'])?$app_android_update['code']:'';
        $info['android_update_url'] = isset($app_android_update['download_url'])?$app_android_update['download_url']:'';
        $info['android_update_description'] = isset($app_android_update['description'])?$app_android_update['description']:'';*/

        $data = array(
            'info' => $info,
            'title' => '配送参数设置'
        );
		$this->view('setting/config', $data, 'validation,cropit_master,webuploader');
	}

    /**
     * 保存数据
     */
    private function _save(){
        $vConf = array(
            array('sharp_title', '分享标题', 'required'),
            array('sharp_url', '分享地址', 'required'),
            array('sharp_desc', '分享描述', 'required'),
            array('map_key', '地图key', 'required'),
            array('customer_tel', '客户电话', 'required'),
            array('min_withdraw_cash_amount', '最小提现金额 ', 'required'),
            array('deliveryman_range', '接单范围', 'required'),
            array('rob_expect_time', '抢单过期时间', 'required'),
            array('base_distribution_fee', '基础配送费', 'required'),
            array('base_distribution_distance', '基础配送距离', 'required'),
            array('base_distribution_weight', '基础配送重量', 'required'),
            array('exceed_base_distance_number', '超过基础距离', 'required'),
            array('exceed_base_distance', '超过基础距离收取费用', 'required'),
            array('exceed_base_weight_number', '基础超过重量', 'required'),
            array('exceed_base_weight', '超过基础重量收取费用', 'required'),
        );
        $this->formDataValidation($vConf);
        $comment = post('comment');
        $comment = isset($comment) ? json_encode($comment, JSON_UNESCAPED_UNICODE) : '';

        /*$appios = array('version' => trim(post('ios_version')),'download_url' => trim(post('ios_update_url')),'description' => trim(post('ios_update_description')),'is_forced' => trim(post('ios_is_forced')));

        $appAndroid = array('version' => trim(post('android_version')),'code' => trim(post('android_version_code')),'download_url' => trim(post('android_update_url')),'description' => trim(post('android_update_description')),'is_forced' => trim(post('android_is_forced')));
        */
        $data = array(
            'sharp_title' => trim(post('sharp_title')),
            'sharp_url' => trim(post('sharp_url')),
            'sharp_desc' => trim(post('sharp_desc')),
            'map_key' => trim(post('map_key')),
            'customer_tel' => trim(post('customer_tel')),
            'min_withdraw_cash_amount' => trim(post('min_withdraw_cash_amount')),
            'deliveryman_range' => trim(post('deliveryman_range')),
            'rob_expect_time' => trim(post('rob_expect_time')),
            'base_distribution_fee' => trim(post('base_distribution_fee')),
            'base_distribution_distance' => post('base_distribution_distance'),
            'base_distribution_weight' => trim(post('base_distribution_weight')),
            'exceed_base_distance_number' => trim(post('exceed_base_distance_number')),
            'exceed_base_distance' => trim(post('exceed_base_distance')),
            'exceed_base_weight_number' => trim(post('exceed_base_weight_number')),
            'exceed_base_weight' => trim(post('exceed_base_weight')),
            'postion_time' => (int)post('postion_time'),
            'deliveryman_comment_tags' => $comment,
            'android_version' => trim(post("android_version")),
            'android_update_description' => trim(post("android_update_description")),
            'android_is_forced' => trim(post("android_is_forced")),
            'android_update_url' => trim(post("android_update_description")),
            'ios_version' => trim(post("ios_version")),
            'ios_update_description' => trim(post("ios_update_description")),
            'ios_is_forced' => trim(post("ios_is_forced")),
            'ios_update_url' => trim(post("ios_update_url")),
        );
        $icon = post('icon');
        if($icon){
            $path = 'uploads/admin/config/share/icon/';
            $tA = saveCSVimg(array($icon), $path, 'icon');
            if( ! $tA){
                $this->ajaxReturn(0, '图标上传失败');
            }
            $data['sharp_icon'] = $tA[0];
        }
        if($this->mSetting->update($this->mSetting->tbDlnSetting, $data)){
            $this->ajaxReturn(1, '保存成功');
        }
        $this->ajaxReturn(0, '保存失败');
    }
}
